geoFacets of Credit Quality

Published

January 2, 2023

A GeoFacet View of Credit Quality

In previous work with Skip Krueger, we conceptualized bond ratings as a multiple rater problem and extracted measure of state level creditworthiness. I had always had it on my list to do something like this and recently ran across a package called geofacet that makes it easy to do. The end result should parse out state level credit risk and showcase the time series of credit risk for each of the states.

So here goes.

Code
library(haven)
library(dplyr)
library(geofacet)
Pew.Data <- read_dta(url("https://github.com/robertwwalker/academic-mymod/raw/master/data/Pew/modeledforprediction.dta"))
library(tidyverse)
load(url("https://github.com/robertwwalker/academic-mymod/raw/master/data/Pew/Scaled-BR-Pew.RData"))
state.ratings <- data.frame(state_name=Pew.Data$state, statefips=Pew.Data$statefips, year=Pew.Data$fyear, BR.Data)
state.ratings.long <- tidyr::gather(state.ratings, sampleno, value, -statefips, -year, -state_name)
state.SE <- state.ratings.long %>% group_by(state_name,year) %>% summarise(Credit.Quality=mean(value), t1=quantile(value, probs=0.025), t2=quantile(value, probs=0.975))
fips <-  data.frame(
stringsAsFactors = FALSE,
state_name = c("Alabama","Alaska","Arizona",
         "Arkansas","California","Colorado","Connecticut",
         "Delaware","Florida","Georgia","Hawaii","Idaho",
         "Illinois","Indiana","Iowa","Kansas","Kentucky","Louisiana",
         "Maine","Maryland","Massachusetts","Michigan",
         "Minnesota","Mississippi","Missouri","Montana","Nebraska",
         "Nevada","New Hampshire","New Jersey","New Mexico",
         "New York","North Carolina","North Dakota","Ohio",
         "Oklahoma","Oregon","Pennsylvania","Rhode Island",
         "South Carolina","South Dakota","Tennessee","Texas","Utah",
         "Vermont","Virginia","Washington","West Virginia",
         "Wisconsin","Wyoming","American Samoa","Guam",
         "Northern Mariana Islands","Puerto Rico","Virgin Islands"),
state = c("AL","AK","AZ","AR","CA",
                "CO","CT","DE","FL","GA","HI","ID","IL","IN","IA",
                "KS","KY","LA","ME","MD","MA","MI","MN","MS",
                "MO","MT","NE","NV","NH","NJ","NM","NY","NC","ND",
                "OH","OK","OR","PA","RI","SC","SD","TN","TX",
                "UT","VT","VA","WA","WV","WI","WY","AS","GU","MP",
                "PR","VI"),
fips = c("01","02","04","05","06",
         "08","09","10","12","13","15","16","17","18","19",
         "20","21","22","23","24","25","26","27","28",
         "29","30","31","32","33","34","35","36","37","38",
         "39","40","41","42","44","45","46","47","48",
         "49","50","51","53","54","55","56","60","66","69",
         "72","78"))  
Res1 <- left_join(state.SE, fips, by=c("state_name" = "state_name"))
Plot1 <- Res1 %>% ggplot(., aes(x=year, y=Credit.Quality, group=state)) +
  geom_pointrange(aes(ymin=t1, ymax=t2, colour=state, fill=state), alpha=0.1) + 
  geom_line(aes(colour=state)) +
  guides(color="none", fill="none") +
  facet_geo(~ state) +
  theme_minimal() + 
  theme(axis.text.x = element_text(size=4, angle=45), axis.text.y = element_text(size=6)) + labs(title="Credit Quality in the US States")
Plot1

Not Long Ago I Learned

One thing that I quite liked about RMarkdown was column figures in the Tufte document templates. Those features are native to quarto, to a large extent. All we need do is add the following bit to code chunks.

#| column: margin
Code
Plot1
Figure 1: Credit quality in US States

Same with tables.

Code
knitr::kable(
  Res1[1:3, 1:3]
)
state_name year Credit.Quality
Alabama 1994 -0.3564650
Alabama 1995 -0.3576466
Alabama 1996 -0.3559865

References

Code
knitr::write_bib(names(sessionInfo()$otherPkgs), file="bibliography.bib")

References

Grolemund, Garrett, and Hadley Wickham. 2011. “Dates and Times Made Easy with lubridate.” Journal of Statistical Software 40 (3): 1–25. https://www.jstatsoft.org/v40/i03/.
Hafen, Ryan. 2025. Geofacet: Ggplot2 Faceting Utilities for Geographical Data. https://github.com/hafen/geofacet.
Müller, Kirill, and Hadley Wickham. 2026. Tibble: Simple Data Frames. https://tibble.tidyverse.org/.
Spinu, Vitalie, Garrett Grolemund, and Hadley Wickham. 2024. Lubridate: Make Dealing with Dates a Little Easier. https://lubridate.tidyverse.org.
Wickham, Hadley. 2016. Ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York. https://ggplot2.tidyverse.org.
———. 2023. Tidyverse: Easily Install and Load the Tidyverse. https://tidyverse.tidyverse.org.
———. 2025a. Forcats: Tools for Working with Categorical Variables (Factors). https://forcats.tidyverse.org/.
———. 2025b. Stringr: Simple, Consistent Wrappers for Common String Operations. https://stringr.tidyverse.org.
Wickham, Hadley, Mara Averick, Jennifer Bryan, Winston Chang, Lucy D’Agostino McGowan, Romain François, Garrett Grolemund, et al. 2019. “Welcome to the tidyverse.” Journal of Open Source Software 4 (43): 1686. https://doi.org/10.21105/joss.01686.
Wickham, Hadley, Winston Chang, Lionel Henry, Thomas Lin Pedersen, Kohske Takahashi, Claus Wilke, Kara Woo, Hiroaki Yutani, Dewey Dunnington, and Teun van den Brand. 2025. Ggplot2: Create Elegant Data Visualisations Using the Grammar of Graphics. https://ggplot2.tidyverse.org.
Wickham, Hadley, Romain François, Lionel Henry, Kirill Müller, and Davis Vaughan. 2023. Dplyr: A Grammar of Data Manipulation. https://dplyr.tidyverse.org.
Wickham, Hadley, and Lionel Henry. 2026. Purrr: Functional Programming Tools. https://purrr.tidyverse.org/.
Wickham, Hadley, Jim Hester, and Jennifer Bryan. 2025. Readr: Read Rectangular Text Data. https://readr.tidyverse.org.
Wickham, Hadley, Evan Miller, and Danny Smith. 2025. Haven: Import and Export SPSS, Stata and SAS Files. https://haven.tidyverse.org.
Wickham, Hadley, Davis Vaughan, and Maximilian Girlich. 2025. Tidyr: Tidy Messy Data. https://tidyr.tidyverse.org.